package com.azkj.album.utils;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.core.content.FileProvider;

import com.azkj.album.MyApplication;
import com.azkj.album.network.utils.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShareUtils {
    /**
     * 微信好友分享-多图片
     * 微信6.7.3后 不再支持分享多图到朋友圈
     */
    public static void shareWeChatByImgList(ArrayList<File> files) {
        if (!isWxExited()) {
            ToastUtils.showCenterToast("请先安装微信！");
            return;
        }
        try {
            Intent share_intent = new Intent();
            ArrayList<Uri> imageUris = new ArrayList<Uri>();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                for (File f : files) {
                    String imageContentUri = getFileUri(MyApplication.getApplication(), f.getAbsolutePath());
                    imageUris.add(Uri.parse(imageContentUri));
                }
            } else {
                for (File f : files) {
                    imageUris.add(Uri.fromFile(f));
                }
            }
            share_intent.setAction(Intent.ACTION_SEND_MULTIPLE);//设置分享行为
            share_intent.setType("image/*");//设置分享内容的类型
            share_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            share_intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
            MyApplication.getCurrentActivity().startActivity(Intent.createChooser(share_intent, "Share"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getFileUri(Context context, String filePath) {
//        String filePath = context.getExternalFilesDir(null) + "/shareData/test.png";
        // 该filePath对应于xml/file_provider_paths里的第一行配置：，因此才可被共享
        File file = new File(filePath);
//        String contentPath = getFileUri(context, file);
        // 使用contentPath作为文件路径进行分享
        // 要与`AndroidManifest.xml`里配置的`authorities`一致，假设你的应用包名为com.example.app
        Uri contentUri = FileProvider.getUriForFile(context,
                "com.azkj.album.provider",file);
        context.grantUriPermission("com.ss.android.ugc.aweme",
                contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.grantUriPermission("com.ss.android.ugc.aweme.lite",
                contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        return contentUri.toString();   // contentUri.toString() 即是以"content://"开头的用于共享的路径
    }


    public static class WeightBean {

        public List<Integer> mComWeights;
        public int mTotalWeight;

        public String totalMoney;
    }

    public static boolean isWxExited() {
        final PackageManager packageManager = MyApplication.getApplication().getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

}
